package analisador;

import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Random;
import java.util.Set;
import java.util.StringTokenizer;
import util.A2ZFileWriter;
import util.Arquivos;

public class MiniMundoBayes {

    //map com todos os tokens com o número de ocorrência
    static Map<String, Integer> mapTokens;
    static List<String> listaOrdenada;
    static Map<String, Collection<String>> listaAdjacenciaAutor = new HashMap<String, Collection<String>>();
    static Map<String, Set<Integer>> mapMensagensAutor = new HashMap<String, Set<Integer>>();
    static Map<String, List<String>> mapIssueAutor = new HashMap<String, List<String>>();
    static Map<String, StringBuilder> mapComentario = new HashMap<String, StringBuilder>();
    static ArrayList<Boolean> lista;
    static Connection con;
    static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
    static int branco = 0;

    public static void main(String[] args) throws Exception {
        //MiniMundoBayes.gerarTreino();
        //MiniMundoBayes.gerarIssues();
//        apagarArquivos();
    }

    public static void conectar() throws Exception {
//        con = DriverManager.getConnection(
//                "jdbc:mysql://localhost:3306/aluno",
//                "root",
//                "root");
        con = DriverManager.getConnection(
                "jdbc:mysql://localhost:3306/hdfs",
                "root",
                "root");
    }

    public static void desconectar() throws Exception {
        con.close();
    }

    public static void gerarTreino() throws Exception {
        apagarArquivos();
        conectar();
        StringBuilder good = new StringBuilder();
        StringBuilder bad = new StringBuilder();

        long tInicial = System.currentTimeMillis();
        Statement stmt = con.createStatement();
        ResultSet rs = stmt.executeQuery("SELECT id, autor, tokens, issue_id FROM comentario where datacomentario > '2009-09-04 00:00:00'");
        while (rs.next()) {
            String autor = rs.getString("autor");
            String comentario = rs.getString("tokens");
            String issue = rs.getString("issue_id");
            if (!mapIssueAutor.containsKey(issue)) {
                mapIssueAutor.put(issue, new ArrayList<String>());
                mapComentario.put(issue, new StringBuilder());
            }
            mapComentario.get(issue).append(comentario).append(" ");
            mapIssueAutor.get(issue).add(autor);
        }

        rs.close();

        List<String> issues = new ArrayList<String>(mapComentario.keySet());

        int num = (int) (0.8 * issues.size());
        Random r = new Random();
        for (int i = 0; i < num; i++) {
            int x = r.nextInt(issues.size());
            String issue = issues.get(x);
            StringBuilder comentario = mapComentario.get(issue);

            if (mapIssueAutor.get(issue).contains("eli")) {
                good.append(comentario);
            } else {
                bad.append(comentario);
            }

            issues.remove(x);
        }
        A2ZFileWriter fw = new A2ZFileWriter("Eli.Good.txt");
        fw.writeContent(good.toString());

        fw = new A2ZFileWriter("Eli.Bad.txt");
        fw.writeContent(bad.toString());

        for (String issue : issues) {
            if (mapIssueAutor.get(issue).contains("eli")) {
                fw = new A2ZFileWriter("bayes/1." + issue + ".txt");
            } else {
                fw = new A2ZFileWriter("bayes/0." + issue + ".txt");
            }
            fw.writeContent(mapComentario.get(issue).toString());
        }
        desconectar();
    }

    public static void gerarIssues() throws Exception {
        conectar();

        long tInicial = System.currentTimeMillis();
        Statement stmt = con.createStatement();
        ResultSet rs;
        rs = stmt.executeQuery("SELECT id,autor,comentario, issue_id FROM  comentario where datacomentario > '2009-09-04 00:00:00';");
        StringBuilder conteudo = new StringBuilder();
        while (rs.next()) {
            long id = rs.getLong("id");
            String comentario = rs.getString("comentario");
            Arquivos.gravar("bayes/" + id + ".txt", comentario, false);
        }

        rs.close();
        desconectar();
    }

    private static void apagarArquivos() {
        File f = new File("./bayes/");
        File[] files =  f.listFiles();
        for(File fi:files){
            fi.delete();
        }
    }
}